/**
 * 后台页js
 */

function backjs() {
  //获取当前页信息
  chrome.tabs.getSelected(null, (tab) => {
    chrome.tabs.executeScript(null, {
      code: `
        var kw = document.querySelector("#kw");
        alert(kw.value);
      `,
    });
  });
}

//右键菜单
chrome.contextMenus.create({
  title: "新增按钮",
  onclick: () => {
    alert(1);
  },
  //匹配网站
  documentUrlPatterns: ["*://*.baidu.com/*"],
  //匹配 可编辑控件/选中内容
  contexts: ["editable", "selection"],
});

//浏览器地址栏输入b+空格搜索
chrome.omnibox.onInputChanged.addListener((text, suggest) => {
  if (!text) return;
  if (text == "herry") {
    suggest([
      { content: text + "视频", description: "你要找herry视频" },
      { content: text + "网站", description: "你要找herry网站" },
    ]);
  }
});
//地址栏回车
chrome.omnibox.onInputEntered.addListener((text) => {
  if (!text) return;
  let href = "";
  href = `https://www.baidu.com/s?wd=${text}`;
  openUrl(href);
});
function getTabId(fn) {
  chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
    if (fn) {
      fn(tabs.length ? tabs[0].id : null);
    }
  });
}
function openUrl(url) {
  getTabId((tabId) => {
    chrome.tabs.update(tabId, { url });
  });
}
